<?php
namespace plugins\address\controller;
use app\common\lib\model_operate\Transit; //引入此类
use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Request;
/**
 * 需求配置首页
 */
class AdminIndexController extends PluginAdminBaseController
{
    /**
     * 初始化
     */
    protected function _initialize()
    {
    }


    


    /**
     * 地址首页
     */
    public function index()
    {
        return $this->fetch();
    }

// $host = "https://ali-city.showapi.com";
//     $path = "/areaName";
//     $method = "GET";
//     $appcode = "你自己的AppCode";
//     $headers = array();
//     array_push($headers, "Authorization:APPCODE " . $appcode);
//     $querys = "areaName=%E4%B8%8A%E6%B5%B7%E5%B8%82&level=1&page=1";
//     $bodys = "";
//     $url = $host . $path . "?" . $querys;

//     $curl = curl_init();
//     curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
//     curl_setopt($curl, CURLOPT_URL, $url);
//     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//     curl_setopt($curl, CURLOPT_FAILONERROR, false);
//     curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//     curl_setopt($curl, CURLOPT_HEADER, true);
//     if (1 == strpos("$".$host, "https://"))
//     {
//         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
//         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//     }
//     var_dump(curl_exec($curl));

    /**
     * 更新地图库
     */
    public function updateAddress()
    {
        ini_set('max_execution_time', '50000');
        ignore_user_abort(true);  // 后台运行
        set_time_limit(0); // 取消脚本运行时间的超时上限
        $address = new \plugins\address\model\AddressModel;
        $res = $address->updateAddress();
        dump($res);
        exit;
        $this->success('更新成功');
    }





    /**
     * 地区主信息页面
     * @param  cri_superior_code   int   上级地区代码
     * @return 首页的所有信息（各类数据）
     */
    public function subord()
    {
        $param = $this->request->param();
        $fa_id   = "";
        $fa_code = "";
        if (empty($param['cri_superior_code'])) {
            $data = Db::name('address')->where('cri_level', 1)->order('cri_sort', 'asc')->select()->each(function ($item, $key) {
                $da = Db::name('address')->where('cri_superior_code', $item['cri_code'])->find();
                if (!empty($da)) {
                    $item['subord'] = 1;
                } else {
                    $item['subord'] = 2;
                }
                return $item;
            });
        } else {
            $cri_superior_code = $param['cri_superior_code'];
            $fa_data = Db::name('address')->where('cri_code', $cri_superior_code)->find();
            if (!empty($fa_data)) {
                $fa_code = $fa_data['cri_superior_code'];
                $fa_id   = $fa_data['cri_id'];
            }
            $data = Db::name('address')->where('cri_superior_code', $cri_superior_code)->order('cri_sort', 'asc')->select()->each(function ($item, $key) {
                $da = Db::name('address')->where('cri_superior_code', $item['cri_code'])->find();
                if (!empty($da)) {
                    $item['subord'] = 1;
                } else {
                    $item['subord'] = 2;
                }
                return $item;
            });
        }
        $this->assign('data', $data);
        $this->assign('fa_id', $fa_id);
        $this->assign('fa_code', $fa_code);
        return $this->fetch();
    }
    /**
     * 添加下级地区页面
     * @param  cri_id   int   当前地区代码
     * @return 数据
     */
    public function addpage()
    {
        $param = $this->request->param();
        if (empty($param['cri_id'])) {
            $this->error('未传递id');
        }
        $data     = Db::name('address')->where('cri_id', $param['cri_id'])->find();
        $superior = (empty($data)) ? '中国' : $data['cri_name'];
        $this->assign('data', $data);
        $this->assign('superior', $superior);
        return $this->fetch();
    }
    /**
     * 添加下级地区操作
     * @param  cri_id           int   上级地区id
     * @param  cri_name         int   添加的地区名称
     * @param  cri_code         int   添加的地区代码
     * @param  cri_short_name   int   添加的地区代码
     * @return 添加成功与否
     */
    public function add()
    {
        $param = $this->request->param();
        if (empty($param['cri_id']) || empty($param['cri_name']) || empty($param['cri_code']) || empty($param['cri_short_name'])) {
            return json(['type' => 'error', 'msg' => '传参错误']);
        }
        $fa_data = Db::name('address')->where('cri_id', intval($param['cri_id']))->find();
        if (empty($fa_data)) {
            return json(['type' => 'error', 'msg' => '查无上级信息']);
        }
        $add['cri_name']          = $param['cri_name']; //名称
        $add['cri_code']          = $param['cri_code']; //地区代码
        $add['cri_short_name']    = $param['cri_short_name']; //地区代码
        $add['cri_superior_code'] = $fa_data['cri_code']; //上级代码
        $re_data = Db::name('address')->where('cri_code', $param['cri_code'])->find();
        if (!empty($re_data)) {
            return json(['type' => 'error', 'msg' => '该地区代码已被使用']);
        }
        $add['cri_memo'] = ''; //备注
        if (!empty($param['cri_memo'])) {
            $add['cri_memo'] = $param['cri_memo'];
        }
        $add['cri_level']      = intval($fa_data['cri_level']) + 1; //等级
        $add['cri_gmt_create'] = date('Y-m-d H:i:s', time()); //创建时间
        $sort                  = Db::name('address')->where('cri_superior_code', $fa_data['cri_code'])->max('cri_sort');
        // $sort = Db::name('address')->where('cri_superior_code',$fa_data['cri_code'])->find();
        $add['cri_sort'] = 0; //排序
        if (isset($sort)) {
            $add['cri_sort'] = intval($sort) + 1; //排序
        }
        $add['status'] = 1; //状态
        $re            = Db::name('address')->insert($add);
        if (empty($re)) {
            return json(['type' => 'error', 'msg' => '失败']);
        }
        return json(['type' => 'success', 'msg' => '添加成功']);
    }
    /**
     * 修改信息页面
     * @param  cri_id           int   当前地区id
     * @return 添加成功与否
     */
    public function editpage()
    {
        $param = $this->request->param();
        if (empty($param['cri_id'])) {
            $this->error('未传递cri_id');
        }
        $data          = Db::name('address')->where('cri_id', $param['cri_id'])->find();
        $superior_data = Db::name('address')->where('cri_code', $data['cri_superior_code'])->find();
        $superior      = (empty($superior_data)) ? '中国' : $superior_data['cri_name'];
        $this->assign('data', $data);
        $this->assign('superior', $superior);
        return $this->fetch();
    }
    /**
     * 修改信息操作
     * @param  cri_id           int   当前地区id
     * @param  cri_name         int   修改后当前地区名称
     * @param  cri_code         int   修改后当前地区代码
     * @param  cri_short_name   int   修改后当前地区代码
     * @return 修改成功与否
     */
    public function edit()
    {
        $param = $this->request->param();
        if (empty($param['cri_id']) || empty($param['cri_name']) || empty($param['cri_code']) || empty($param['cri_short_name'])) {
            return json(['type' => 'error', 'msg' => '传参错误']);
        }
        $update['cri_id']         = $param['cri_id'];
        $update['cri_name']       = $param['cri_name'];
        $update['cri_code']       = $param['cri_code'];
        $update['cri_short_name'] = $param['cri_short_name'];
        $re_data                  = Db::name('address')->where('cri_code', $param['cri_code'])->find();
        if ($re_data) {
            if ($param['cri_id'] != $re_data['cri_id']) {
                return json(['type' => 'error', 'msg' => '该地区代码已被使用']);
            }
        }
        $re = Db::name('address')->update($update);
        if (empty($re)) {
            return json(['type' => 'error', 'msg' => '编辑失败']);
        }
        return json(['type' => 'success', 'msg' => '编辑成功']);
    }
    /**
     * 地区操作 --删除
     * @param  cri_id           int   当前地区id
     * @return 删除成功与否
     */
    public function del()
    {
        $param = $this->request->param();
        if (empty($param['cri_id'])) {
            $this->error("传值错误");
        }
        $cri_id = $param['cri_id'];
        $re = Db::name('address')->where('cri_id', $cri_id)->delete();
        if (empty($re)) {
            $this->error("删除失败");
        }
        $this->success("删除成功");
    }
    /**
     * 地区操作--启用-显示
     * @param  cri_id           int   当前地区id
     * @return 操作成功与否
     */
    public function turn_on()
    {
        $param = $this->request->param();
        if (empty($param['cri_id'])) {
            $this->error("传值错误");
        }
        $update['cri_id'] = $param['cri_id'];
        $data             = Db::name('address')->where('cri_id', $update['cri_id'])->find();
        if ($data['status'] == 1) {
            $this->error("无需多次显示");
        }
        $update['status'] = 1;
        $re               = Db::name('address')->update($update);
        if (empty($re)) {
            $this->error("显示除失败");
        }
        $this->success("显示成功");
    }
    /**
     * 地区操作--禁用-隐藏
     * @param  cri_id           int   当前地区id
     * @return 操作成功与否
     */
    public function turn_off()
    {
        $param = $this->request->param();
        if (empty($param['cri_id'])) {
            $this->error("传值错误");
        }
        $update['cri_id'] = $param['cri_id'];
        $data             = Db::name('address')->where('cri_id', $update['cri_id'])->find();
        if ($data['status'] == 2) {
            $this->error("无需多次隐藏");
        }
        $update['status'] = 2;
        $re               = Db::name('address')->update($update);
        if (empty($re)) {
            $this->error("隐藏失败");
        }
        $this->success("隐藏成功");
    }
    /**
     * 地区操作--排序操作 （jquery传值）
     * @param  key              array   当前页地区的cri_id数组
     * @param  value            array   当前页地区的cri_sort数组
     * @return 操作成功与否
     */
    public function sort()
    {
        $param = $this->request->param();
        if (empty($param['key']) || empty($param['value'])) {
            return json(['type' => 'error', 'msg' => '无值']);
        }
        $keys   = explode(",", $param['key']);
        $values = explode(",", $param['value']);
        foreach ($keys as $k => $v) {
            $data[$v] = intval($values[$k]);
        }
        foreach ($data as $key => $value) {
            $update = [];
            $re     = '';
            $update = ['cri_id' => $key, 'cri_sort' => $value];
            $re     = Db::name('address')->update($update);
        }
        return json(['type' => 'success', 'msg' => '更新完成']);
    }
}
